package com.yiwei.jmx;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.support.multidex.MultiDex;
import android.util.Log;


import com.bugtags.library.Bugtags;
//import com.taobao.sophix.PatchStatus;
//import com.taobao.sophix.SophixManager;
//import com.taobao.sophix.listener.PatchLoadStatusListener;
import com.yiwei.jmx.constant.Const;
import com.yiwei.jmx.util.PreferenceUtils;
import com.mob.MobSDK;
import com.umeng.analytics.MobclickAgent;
import com.yiwei.jmx.util.ToastUtil;
import com.yiwei.jmx.util.Util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Locale;

import cn.jpush.android.api.JPushInterface;
import kangcheng.com.lmzx_android_sdk_v10.commom.sdk.LmzxSdkImpl;

//import com.bugtags.library.Bugtags;

/**
 * Created by xsooy-pc on 17-12-6.
 */

public class Application extends android.app.Application {
    private static Application instance;
    private LmzxSdkImpl sdk;

    public static Application getInstance() {
        return instance;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        JPushInterface.setDebugMode(true);
        JPushInterface.init(this);

        //禁止默认页面统计==友盟
        MobclickAgent.openActivityDurationTrack(false);

//        ShareSdk
        MobSDK.init(this, Const.MobSdk.KEY, Const.MobSdk.SECRET);

//        Log.i("dasdasdsa", sHA1(this));
        //        BUGS提交



        sdk = LmzxSdkImpl.getInstance();
        if (Const.IS_DEBUG) {
//            sdk.setEnv(this, "https://t.limuzhengxin.cn:3443/api/gateway");
            sdk.setEnv(this, "https://api.limuzhengxin.com/api/gateway");
            Bugtags.start("2bd3005cfa53ecc22a8632bb55d79d53", this, Bugtags.BTGInvocationEventBubble);
//            sdk.setEnv(BaseContext, "https://t.limuzhengxin.cn/api/gateway");
        } else {
            sdk.setEnv(this, "https://api.limuzhengxin.com/api/gateway");
        }
//        SophixManager.getInstance().queryAndLoadNewPatch();
    }

    public void LmsdkInit(String apikey, String uid) {
        sdk.init(this, apikey, uid, Const.SERVER_API_URL + "/callback/limu");//apikey 必须的
    }

    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
//        SophixManager.getInstance().setContext(this)
//                .setAppVersion(Util.getVersion(this))
////                .setAesKey(null)
//                .setEnableDebug(true)
//                .setPatchLoadStatusStub(new PatchLoadStatusListener() {
//                    @Override
//                    public void onLoad(final int mode, final int code, final String info, final int handlePatchVersion) {
//                        // 补丁加载回调通知
//                        if (code == PatchStatus.CODE_LOAD_SUCCESS) {
//                            // 表明补丁加载成功
//                            ToastUtil.makeText(instance, "加载成功");
//                        } else if (code == PatchStatus.CODE_LOAD_RELAUNCH) {
//                            // 表明新补丁生效需要重启. 开发者可提示用户或者强制重启;
//                            // 建议: 用户可以监听进入后台事件, 然后调用killProcessSafely自杀，以此加快应用补丁，详见1.3.2.3
//                            ToastUtil.makeText(instance, "重启应用");
//                            SophixManager.getInstance().killProcessSafely();
//                        } else {
//                            // 其它错误信息, 查看PatchStatus类说明
//                            ToastUtil.makeText(instance, "错误代码：" + code);
//                        }
//                    }
//                }).initialize();
    }

    public static String sHA1(Context context) {
        try {
            PackageInfo info = context.getPackageManager().getPackageInfo(
                    context.getPackageName(), PackageManager.GET_SIGNATURES);
            byte[] cert = info.signatures[0].toByteArray();
            MessageDigest md = MessageDigest.getInstance("SHA1");
            byte[] publicKey = md.digest(cert);
            StringBuffer hexString = new StringBuffer();
            for (int i = 0; i < publicKey.length; i++) {
                String appendString = Integer.toHexString(0xFF & publicKey[i])
                        .toUpperCase(Locale.US);
                if (appendString.length() == 1)
                    hexString.append("0");
                hexString.append(appendString);
                hexString.append(":");
            }
            String result = hexString.toString();
            return result.substring(0, result.length() - 1);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return null;
    }
}
